# -*- coding: utf-8 -*-
import scrapy
from scrapy.http import Request
from urllib import parse
from ArticleSpider.items import StarArticleItem,StarArticleItemLoader

class StarSpider(scrapy.Spider):
    name = 'star'
    allowed_domains = ['music.taihe.com']
    start_urls = ['http://music.taihe.com/artist/cn/male']

    def parse(self, response):
        post_nodes = response.css(".container .list-item ul li a")
        i=0
        for post_node in post_nodes:
            i=i+1
            if i>5:
                break
            post_url = post_node.css("::attr(href)").extract_first() #取第一个匹配值
            yield Request(url=parse.urljoin(response.url,post_url),callback=self.parse_detail)

    def parse_detail(self,response):
        imgurl = response.css(".artist-img-box img ::attr(src)").extract_first()
       # print (imgurl)

        item_loader = StarArticleItemLoader(item=StarArticleItem(),response=response)
        item_loader.add_css("starname",".title .artist-name")
        item_loader.add_value("headimg",[imgurl])
        star_item = item_loader.load_item()
        yield star_item
        pass
